<!--
 * @Descripttion: 
 * @LastEditors: xzh
 * @LastEditTime: 2023-11-04 22:20:00
-->
<template>
  <DileDrawer
    v-model="outBoundDrawerParam.visible"
    :loading="outBoundDrawerParam.loading"
    size="80%"
    @sure="hanldeSure"
  >
    <template #header>
      <span>{{
        $t(`table.btnArr.${!outBoundDrawerParam.id ? "add" : "edit"}`)
      }}</span>
    </template>
    <template #default>
      <!-- 模具出库表单 -->
      <OutBoundForm ref="outBoundFormRef" />
    </template>
  </DileDrawer>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick, toRefs } from "vue";
import { object_Inf } from "@/utils/typings/index.type";
import OutBoundForm from "../index.vue";
import DileDrawer from "@/components/dile-drawer/index.vue";
const emit = defineEmits(["refresh"]);
/**
 * @Author: xzh
 * @Descripttion: 设备出库表单实例
 * @Param:
 */
let outBoundFormRef = ref<any>(null);
/**
 * @Author: xzh
 * @Descripttion: 记录抽屉参数
 * @Param:
 */
let outBoundDrawerParam = reactive<object_Inf>({
  visible: false,
  loading: false,
  id: null,
});

/**
 * @Author: xzh
 * @Descripttion:初始化加载
 * @Param:
 * @param {*} id 保养计划主键
 */
let init = async (id: number) => {
  outBoundDrawerParam.visible = true;
  await nextTick();
  try {
    Object.assign(outBoundDrawerParam, {
      loading: true,
      id,
    });
    //加载表单数据
    await outBoundFormRef.value.initFormData(id);
  } finally {
    outBoundDrawerParam.loading = false;
  }
};
/**
 * @Author: xzh
 * @Descripttion: 保存事件
 * @Param:
 */
let hanldeSure = async () => {
  let { loading, visible } = toRefs(outBoundDrawerParam);
  if (!!loading.value) {
    return false;
  }
  try {
    loading.value = true;
    //保存表单数据
    if ((await outBoundFormRef.value.hanldeSaveForm()) == false) {
      return false;
    }
    //触发刷新回调
    emit("refresh");
    //关闭弹框
    visible.value = false;
  } finally {
    loading.value = false;
  }
};
defineExpose({
  init,
});
</script>
<style lang="scss" scoped></style>
